package cn.iocoder.yudao.module.measure.controller.admin.point;


import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.measure.controller.admin.point.VO.*;
import cn.iocoder.yudao.module.measure.dal.dataobject.point.DtuInfoDO;
import cn.iocoder.yudao.module.measure.dal.dataobject.point.MeasurePointInfoDO;
import cn.iocoder.yudao.module.measure.service.point.BusinessDtuInfoService;
import cn.iocoder.yudao.module.measure.service.point.BusinessMeasurePointInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - DTU信息")
@RestController
@RequestMapping("/measure")
@Validated
@Slf4j
public class MeasurePointController {

    @Resource
    private BusinessDtuInfoService dtuInfoService;

    @Resource
    private BusinessMeasurePointInfoService measurePointInfoService;


    @GetMapping("/dtu/page")
    @Operation(summary = "获得DTU分页")
    @PreAuthorize("@ss.hasPermission('measure:dtu:query')")
    public CommonResult<PageResult<DtuInfoRespVO>> queryDtuPage(@Valid DtuInfoPageReqVO pageReqVO){
//      getLoginUserId()
        PageResult<DtuInfoDO> pageResult = dtuInfoService.getDtuInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DtuInfoRespVO.class));
    }



    @PostMapping("/dtu/create")
    @Operation(summary = "创建dtu信息")
    @PreAuthorize("@ss.hasPermission('measure:dtu:add')")
    public CommonResult<Long> createDtuInfo(@Valid @RequestBody DtuInfoSaveReqVO createReqVO) {
        return success(dtuInfoService.createDtuInfo(createReqVO));
    }




    @PutMapping("/dtu/update")
    @Operation(summary = "更新dtu信息")
    @PreAuthorize("@ss.hasPermission('measure:dtu:edit')")
    public CommonResult<Boolean> updateDtuInfo(@Valid @RequestBody DtuInfoSaveReqVO updateReqVO) {
        dtuInfoService.updateDtuInfo(updateReqVO);
        return success(true);
    }




    @DeleteMapping("/dtu/delete")
    @Operation(summary = "删除dtu信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:dtu:delete')")
    public CommonResult<Boolean> deleteDtuInfo(@RequestParam("id") Long id) {
        dtuInfoService.deleteDtuInfo(id);
        return success(true);
    }

    @DeleteMapping("/dtu/deleteList")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除dtu信息")
    @PreAuthorize("@ss.hasPermission('business:dtu:delete')")
    public CommonResult<Boolean> deleteDtuInfoList(@RequestParam("ids") List<Long> ids) {
        dtuInfoService.deleteDtuInfoListByIds(ids);
        return success(true);
    }


    @GetMapping("/dtu/get")
    @Operation(summary = "获得dtu信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:dtu:query')")
    public CommonResult<DtuInfoRespVO> getDtuInfo(@RequestParam("id") Long id) {
        DtuInfoDO dtuInfo = dtuInfoService.getDtuInfo(id);
        return success(BeanUtils.toBean(dtuInfo, DtuInfoRespVO.class));
    }




    @GetMapping("/dtu/exportExcel")
    @Operation(summary = "导出dtu信息 Excel")
    @PreAuthorize("@ss.hasPermission('business:dtu:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDtuInfoExcel(@Valid DtuInfoPageReqVO pageReqVO,
                                   HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DtuInfoDO> list = dtuInfoService.getDtuInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "dtu信息.xls", "数据", DtuInfoRespVO.class,
                BeanUtils.toBean(list, DtuInfoRespVO.class));
    }


    @PostMapping("/point/create")
    @Operation(summary = "创建测量点信息")
    @PreAuthorize("@ss.hasPermission('measure:point:add')")
    public CommonResult<Long> createMeasurePointInfo(@Valid @RequestBody MeasurePointInfoSaveReqVO createReqVO) {
        return success(measurePointInfoService.createMeasurePointInfo(createReqVO));
    }

    @PutMapping("/point/update")
    @Operation(summary = "更新测量点信息")
    @PreAuthorize("@ss.hasPermission('measure:point:edit')")
    public CommonResult<Boolean> updateMeasurePointInfo(@Valid @RequestBody MeasurePointInfoSaveReqVO updateReqVO) {
        measurePointInfoService.updateMeasurePointInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/point/delete")
    @Operation(summary = "删除测量点信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('measure:point:delete')")
    public CommonResult<Boolean> deleteMeasurePointInfo(@RequestParam("id") Long id) {
        measurePointInfoService.deleteMeasurePointInfo(id);
        return success(true);
    }

    @DeleteMapping("/point/deleteList")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除测量点信息")
    @PreAuthorize("@ss.hasPermission('measure:point:delete')")
    public CommonResult<Boolean> deleteMeasurePointInfoList(@RequestParam("ids") List<Long> ids) {
        measurePointInfoService.deleteMeasurePointInfoListByIds(ids);
        return success(true);
    }

    @GetMapping("/point/get")
    @Operation(summary = "获得测量点信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('measure:point:query')")
    public CommonResult<MeasurePointInfoRespVO> getMeasurePointInfo(@RequestParam("id") Long id) {
        MeasurePointInfoDO measurePointInfo = measurePointInfoService.getMeasurePointInfo(id);
        return success(BeanUtils.toBean(measurePointInfo, MeasurePointInfoRespVO.class));
    }

    @GetMapping("/point/page")
    @Operation(summary = "获得测量点信息分页")
    @PreAuthorize("@ss.hasPermission('measure:point:query')")
    public CommonResult<PageResult<MeasurePointInfoRespVO>> getMeasurePointInfoPage(@Valid MeasurePointInfoPageReqVO pageReqVO) {
        PageResult<MeasurePointInfoDO> pageResult = measurePointInfoService.getMeasurePointInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MeasurePointInfoRespVO.class));
    }

    @GetMapping("/point/exportExcel")
    @Operation(summary = "导出测量点信息 Excel")
    @PreAuthorize("@ss.hasPermission('measure:point:query')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMeasurePointInfoExcel(@Valid MeasurePointInfoPageReqVO pageReqVO,
                                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MeasurePointInfoDO> list = measurePointInfoService.getMeasurePointInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "测量点信息.xls", "数据", MeasurePointInfoRespVO.class,
                BeanUtils.toBean(list, MeasurePointInfoRespVO.class));
    }




}
